package com.example.notification;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button button1;
    Button button2;

    NotificationManager notificationManager;
    Notification notification;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);

        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

//        // 版本判断
//        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
//            NotificationChannel notificationChannel =
//                    new NotificationChannel("leo", "通知信息", NotificationManager.IMPORTANCE_DEFAULT);
//            notificationManager.createNotificationChannel(notificationChannel);
//        }

        // 发布一个通知
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                notification = new NotificationCompat.Builder(MainActivity.this,"leo")
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentTitle("标题")
                        .setContentText("这是一条消息")
                        .setPriority(Notification.PRIORITY_HIGH)
                        .build();


//                notification = new Notification.Builder(MainActivity.this)
//                        .setSmallIcon(R.drawable.ic_launcher_background)
//                        .setContentTitle("标题")
//                        .setContentText("这是一条消息")
//                        .setPriority(Notification.PRIORITY_HIGH)
//                        .build();
                notificationManager.notify(1, notification);
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }
}